<?php

/**
 * This is the model class for table "tbl_item".
 *
 * The followings are the available columns in table 'tbl_item':
 * @property integer $id
 * @property string $name
 * @property string $avatar
 * @property integer $status
 * @property integer $user_created
 * @property string $created_at
 * @property integer $user_updated
 * @property string $updated_at
 *
 * The followings are the available model relations:
 * @property ImageTag[] $imageTag
 * @property Admin $userCreated
 * @property Admin $userUpdated
 */
class Item extends CActiveRecord {
    const ITEM_ACTIVE = 1;
    const ITEM_LOCKED = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Item the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_item';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, avatar, user_created, created_at', 'required'),
            array('status, user_created, user_updated', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 40),
            array('avatar', 'length', 'max' => 100),
//            array('avatar', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => true, 'on' => 'update'),
            array('avatar', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => false, 'on' => 'create'),
            array('updated_at', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, avatar, status, user_created, created_at, user_updated, updated_at', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'imageTag' => array(self::HAS_MANY, 'ImageTag', 'item_id'),
            'userCreated' => array(self::BELONGS_TO, 'Admin', 'user_created'),
            'userUpdated' => array(self::BELONGS_TO, 'Admin', 'user_updated'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Tên',
            'avatar' => 'Ảnh',
            'status' => 'Trạng thái',
            'user_created' => 'Người tạo',
            'created_at' => 'Ngày tạo',
            'user_updated' => 'Người cập nhật',
            'updated_at' => 'Ngày cập nhật',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('avatar', $this->avatar, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('user_created', $this->user_created);
        $criteria->compare('created_at', $this->created_at, true);
        $criteria->compare('user_updated', $this->user_updated);
        $criteria->compare('updated_at', $this->updated_at, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}